package com.tywj.panda.customer.common.http

import org.json.JSONObject

/**
 * @desc 网络请求回调，返回数据类型可能有多种
 * @author June Yang
 * @date 2017/9/2
 */
abstract class PDMultipleDataCallback(
    isHandleErrorSelf: Boolean = true
) : PDBaseCallback(isHandleErrorSelf) {

    override fun onSuccess(data: String) {
        val responseData = JSONObject(data)
        val code = responseData.getInt("code")
        val message = responseData.getString("msg")

        if (code == 1000 || code >= 1400) {
            try {
                success(code, message, responseData.getJSONObject("data").toString())
            } catch (e: Exception) {
                success(code, message, responseData.getJSONArray("data").toString())
            }
        } else {
            // 全局错误
            handleAsyncRequestError(code, message)
        }
    }

    abstract fun success(code: Int, message: String, resp: String)
}